package io.renren.controller;

import io.renren.common.utils.ConfigConstant;
import io.renren.common.utils.R;
import io.renren.common.validator.Assert;
import io.renren.service.SysConfigService;
import io.renren.service.SysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 配置接口
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-23 15:31
 */
@RestController
@RequestMapping("/api/config")
@Api(tags = "配置接口")
public class ApiConfigController extends AbstractController {

	@Autowired
	private SysConfigService sysConfigService;

	@Autowired
	private SysDictService sysDictService;

	@PostMapping("init")
	@ApiOperation("配置参数")
	public R init() {
		Map<String, Object> map = new HashMap<>();
		map.put("userLevel", sysDictService.selectByType("user_level"));
		map.put("memberStatus", sysDictService.selectByType("member_status"));
		map.put("authAtatus", sysDictService.selectByType("auth_status"));
		map.put("bank", sysDictService.selectByType("bank"));
		map.put("machineType", sysDictService.selectByType("machine_type"));

		map.put("webPage", sysConfigService.getValue(ConfigConstant.WEB_PAGE));
		map.put("mustRecPerson", sysConfigService.getValue(ConfigConstant.MUST_REC_PERSON));
		map.put("orderMaxPrice", sysConfigService.getValue(ConfigConstant.ORDER_MAX_PRICE));
		map.put("coinMinQty", sysConfigService.getValue(ConfigConstant.COIN_MIN_QTY));
		map.put("transFee", sysConfigService.getValue(ConfigConstant.TRANS_FEE));
		map.put("recText", sysConfigService.getValue(ConfigConstant.REC_TEXT));
		map.put("tencentAid", sysConfigService.getValue(ConfigConstant.TENCENT_AID));

		return R.ok(map);
	}

	@PostMapping("checkUpdate")
	@ApiOperation("更新检查")
	public R checkUpdate(String curVersion) {
		Assert.hasText(curVersion, "版本号不能为空");
		String lastVersion = sysConfigService.getValue(ConfigConstant.LAST_VERSION);
		String url = null;
		boolean update = false;
		if (!lastVersion.equals(curVersion)) {
			url = sysConfigService.getValue(ConfigConstant.DOWNLOAD_URL);
			update = true;
		}
		return R.ok().put("update", update).put("url", url);
	}
}
